<?php
/**
 * Created by PhpStorm.
 * User: minicart
 * Date: 2018/12/2
 * Time: 12:16
 */
namespace app\models;

use think\Model;

/**
 * Class GradesModel
 * @package app\models
 */
class GradesDetailsListsModel extends Model
{
    protected $table = 'edu_class_manage_details_lists';

    /**
     * @param $param
     * @return array
     * @throws \think\exception\DbException
     */
    public function getArrangingLists($param)
    {
        $GradesDetailsModel = new GradesDetailsModel();  // 班级
        $page = isset($param['page']) ? (int) $param['page'] : 1;
        $listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
        $where = [];
        $where['graduation_status'] = 0; //未结业
        //班级名称
        if (isset($param['class_grades_name']) && '' !== $param['class_grades_name']) {
            $where['class_grades_name'] = ['like', '%'.$param['class_grades_name'].'%'];
        }


        //课程id
        if (isset($param['course_id']) && '' !== $param['course_id']) {
            $where['course_id'] = $param['course_id'];
        }

        //上课状态1,表示已经上课，0表示未上课
        if (isset($param['status']) && '' !== $param['status']) {
            $where['status'] = $param['status'];
        }

        //校区id
        if (isset($param['campus_id']) && '' !== $param['campus_id']) {
            $where['campus_id'] = $param['campus_id'];
        }else{
			if(get_current_type() == 4 || get_current_type() == 5){
				$where['campus_id'] = get_current_campus_id();
			}elseif (get_current_type() == 2 || get_current_type() == 3){
				$where['campus_id'] = ['in',get_current_campus_id()];
			}
		}

        //试听日期段
        if (isset($param['audition_day']) && '' !== $param['audition_day']) {
            $where['class_date'] = array('egt',$param['audition_day']);
        }


        //试听时间段
        if (isset($param['audition_time']) && '' !== $param['audition_time']) {

        }


        //时间段
        if (isset($param['start_date']) && '' !== $param['start_date'] && isset($param['end_date']) && '' !== $param['end_date']) {
            $where['class_date'] = array('between', array($param['start_date'],$param['end_date']));
        }


        //跟班时间段
        if (isset($param['class_start_date']) && '' !== $param['class_start_date'] && isset($param['class_end_date']) && '' !== $param['class_end_date']) {
            $where['class_date'] = array('between', array($param['class_start_date'],$param['class_end_date']));
        }

        //班级id
        if (isset($param['class_manage_id']) && '' !== $param['class_manage_id']) {
            $where['class_manage_id'] = $param['class_manage_id'];
        }


        //跟班班级id

        if (isset($param['follow_class_manage_id']) && '' !== $param['follow_class_manage_id']) {
            $arr = explode(',',$param['follow_class_manage_id']);
            $arr = array_unique($arr);
            $str = implode(',',$arr);
            $where['class_manage_id'] = array('not in',$str);
        }

        if (isset($param['keywords']) && '' !== $param['keywords']) {
            if(isset($param['audition_type'])){
                if ($param['audition_type'] == ''){
                    $where['class_grades_name'] = ['like', '%'.$param['keywords'].'%'];
                }elseif ($param['audition_type'] == '2'){
                    $where['teacher'] = ['like', '%'.$param['keywords'].'%'];
                }elseif ($param['audition_type'] == '3'){
                    $where['class_room'] = ['like', '%'.$param['keywords'].'%'];
                }
            }
        }

        //获取教室课表
        if(get_current_type() == 5){
            if(get_current_group_type() == 2){  //老师类
                $grades_res = $GradesDetailsModel->where(['user_teacher_id' => get_current_id()])->column('id');
                $grades_res_str = implode(',',$grades_res);
                $where['class_manage_id'] = ['in',$grades_res_str];
            }else if(get_current_group_type() == 3){  //助教类
                $grades_res = $GradesDetailsModel->where(['user_assistant_id' => get_current_id()])->column('id');
                $grades_res_str = implode(',',$grades_res);
                $where['class_manage_id'] = ['in',$grades_res_str];
            }else if(get_current_group_type() == 4){ //班主任
                $grades_res = $GradesDetailsModel->where(['user_master_id' => get_current_id()])->column('id');
                $grades_res_str = implode(',',$grades_res);
                $where['class_manage_id'] = ['in',$grades_res_str];
            }else if(get_current_group_type() == 5){ //教务类
                $grades_res = $GradesDetailsModel->where(['user_edu_id' => get_current_id()])->column('id');
                $grades_res_str = implode(',',$grades_res);
                $where['class_manage_id'] = ['in',$grades_res_str];
            }
        }


        $listData = self::where($where)->order('update_time desc')
            ->paginate($listRows, false, ['page' => $page])
            ->toArray();
        if (!empty($listData)) {
            $StudentSubjectInfosClassModel = new StudentSubjectInfosClassModel();
            $i = 0;
			$num = 0;
			$p = (($page - 1) * $listRows + 1);
            foreach ($listData['data'] as &$item) {
				$num = $i + $p;
				$i++;
                $item['course_name'] = CourseModel::get($item['course_id'])['course_name'];
                $item['campus_name'] = CampusModel::get($item['campus_id'])['campus_name'];
                $res = GradesDetailsModel::get($item['class_manage_id']);
                $item['course_class_name'] = $item['course_name'].'第'.$num.'节';

                if($item['graduation_status'] == 0){
                    $item['graduation_status_text'] = '正常';
                }else if($item['graduation_status'] == 1){
                    $item['graduation_status_text'] = '结业';
                }


                $start = strtotime($item['class_subject_start']);  //开始时间
                $end = strtotime($item['class_subject_end']);  //结束时间
                $item['course_date_week_time'] = $item['class_date'].'-'.$item['weekday'].'-'.$item['class_subject_start'].'-'.$item['class_subject_end'];
                $item['class_length'] = ceil(($end - $start)/60).'分钟';
                $nums = $StudentSubjectInfosClassModel->where(['class_manage_details_id' => $item['id']])->select();
                if($item['status'] == 0){  //未上课
                    $item['actual_number'] = '0/'.count($nums);
                }else if($item['status'] == 1){  //已上课
                    $already_nums = 0;
                    foreach ($nums as $key => $value){

                        if($value['attendance'] == 1){
                            $already_nums ++;
                        }
                    }
                    $item['actual_number'] = $already_nums.'/'.count($nums);
                }
                $item['status_text'] = $item['status'] ? '<span style="color:#01AAED;font-weight: bold;">已上课</span>' : ' <span  style="color:#FF5722;font-weight: bold;">未上课</span>';


                $item['grades_course_class_name'] = $res['grades_name']. '('.$item['course_class_name'].')';
                $item['grades_time'] = $item['class_date'].'---'.$item['class_subject_start'].'-'.$item['class_subject_end'];
                $item['student_nums'] = $res['preoccupation'];
                $result = StudentSubjectClassModel::where(['class_subject_id' => $item['class_manage_id']])->select();
                $student_id_arr = [];
                $nums = 0;
                foreach ($result as $key => $value){
                    if(!in_array($value['student_id'],$student_id_arr)){
                        $nums = $nums + 1;
                        $student_id_arr[] = $value['student_id'];
                    }
                }

                if($item['student_nums'] == 0){
                    $item['student_nums'] = $nums.'/'.'不限';
                }else{
                    $item['student_nums'] = $nums.'/'.$item['student_nums'];
                }
            }
        }
        return $listData;
    }


}